package com.jack.helloworld.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;

import com.jack.helloworld.R;
import com.jack.helloworld.view.DownloadProgressButton;
import com.orhanobut.logger.Logger;

import java.util.Random;

import androidx.appcompat.app.AppCompatActivity;

public class DownloadProgressButtonActivity extends AppCompatActivity {

    private DownloadProgressButton mDownloadProgressButton;
    private Button mBtnReset;
    private CountDownTimer mCountDownTimer;
    private int firstPercent = 0;
    private int MAX = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_progress_button);

        initView();
        initData();
    }

    private void initData() {

        mDownloadProgressButton.setEnablePause(false);
        mDownloadProgressButton.setOnDownLoadClickListener(new DownloadProgressButton.OnDownLoadClickListener() {
            @Override
            public void clickDownload() {
                if (mDownloadProgressButton.getState() == DownloadProgressButton.FINISH) {
                    Logger.d("Download finish!");
                    if (mCountDownTimer != null) {
                        mCountDownTimer.cancel();
                        mCountDownTimer = null;
                    }
                    return;
                }

                mCountDownTimer = new CountDownTimer(10000, 500) {
                    @Override
                    public void onTick(long l) {
                        int progress = new Random().nextInt(20);
                        if (progress + firstPercent >= MAX) {
                            progress = MAX - firstPercent;
                        }
                        Logger.d("DownloadProgressButtonActivity#progress: " + progress);
                        firstPercent += progress;
                        Logger.d("DownloadProgressButtonActivity#firstPercent: " + firstPercent);
                        mDownloadProgressButton.setProgress(mDownloadProgressButton.getProgress() + progress);
                    }

                    @Override
                    public void onFinish() {
                        return;
                    }
                }.start();

            }

            @Override
            public void clickPause() {

            }

            @Override
            public void clickResume() {
                clickDownload();
            }

            @Override
            public void clickFinish() {
            }
        });


        mBtnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                firstPercent = 0;
                mDownloadProgressButton.reset();
            }
        });
    }

    private void initView() {
        mDownloadProgressButton = (DownloadProgressButton) findViewById(R.id.download_button);
        mBtnReset = (Button) findViewById(R.id.download_reset);

    }
}
